$(function(){
    obj.init();
});

var obj = new JsFun();
function JsFun(){
    'use strict';
    return {
        params: {
            type: null,
            schoolId: null,
            id: null,
            ids: null,
            studId: null,
            annualCost: null,
            chargeInfo: null
        },
        dom: {
            save: null,
            devCost: null,
            deposit: null,
            rentCost: null,
            serCost: null,
            sumDues: null,
            sumCost: null,
            serYear: null,
            chargeYear: null,
            chargeTime: null,
            remark: null,
            startTime: null,
            expireTime: null
        },
        form : null,
        init: function () {
            this.form = new xhForm($('#xh_form'));

            //dom绑定
            {
                this.dom.save       = window.top.xhTop.getModalConfirmDom();
                this.dom.devCost    = $('#iDevCost');
                this.dom.deposit    = $('#iDeposit');
                this.dom.rentCost   = $('#iRentCost');
                this.dom.serCost    = $('#iSerCost');
                this.dom.sumDues    = $('#iSumDues');
                this.dom.sumCost    = $('#iSumCost');
                this.dom.serYear    = $('#iSerYear');
                this.dom.chargeYear = $('#iChargeYear');
                this.dom.chargeTime = $('#tChargeTime');
                this.dom.remark     = $('#mRemarks');
                this.dom.startTime  = $('#tStartTime');
                this.dom.expireTime = $('#tExpireTime');
            }

            //参数初始化
            {
                this.params.type        = xhTool.getQuery('type');
                this.params.schoolId    = xhTool.getQuery('schoolId');
                this.params.id          = xhTool.getQuery('id');
                this.params.ids         = xhTool.getQuery('ids');
                this.params.studId      = xhTool.getQuery('studId');
                this.params.annualCost  = parseInt(window.top.xhTop.getConfigByName('studCharge').annualCost) || 360;
                this.params.chargeInfo  = typeof chargeInfo === 'object' ? chargeInfo : {};
            }

            //根据操作类型初始化，绑定监听事件
            switch (this.params.type) {
                case 'create': this.initCreate(); this.listenerCountTotal(); break;
                case 'modify': this.initModify(); this.listenerCountTotal(); break;
                case 'split' : this.initSplit(); break;
                case 'splitAll': this.initSplitAll(); this.listenerCountTotal(); break;
            }

            this.listener();
        },
        initCreate: function () {
            this.initCreateModify();
            this.form.bindRequired(this.dom.save);
        },
        initModify: function () {
            this.initCreateModify();
            this.form.fill(this.params.chargeInfo).bindRequired(this.dom.save);
            this.dom.chargeTime.change();
            this.dom.devCost.prop('readonly', parseInt(this.dom.deposit.val()) > 0);
            this.dom.deposit.prop('readonly', parseInt(this.dom.devCost.val()) > 0);
        },
        initCreateModify: function () {
            //填充缴费id
            $('#iCCId').val(this.params.id);
            //填充学生id
            $('#iStudId').val(this.params.studId);
            //渲染时间选择
            this.dom.chargeTime.datetimepicker({
                format: 'yyyy-mm-dd hh:ii:ss',  //年-月-日 时:分:秒
                autoclose:true,                 //自动关闭
                language:"zh-CN",               //中文
                todayBtn:true,                  //选择今天按钮
                minView:0
            });
        },
        initSplit: function () {
            this.form.fill(this.params.chargeInfo);
            this.dom.chargeYear
                .add(this.dom.chargeTime)
                .add(this.dom.remark)
                .parent().parent()
                .slideUp();
            this.dom.sumDues
                .add(this.dom.sumCost)
                .prop('disabled', true);
            this.dom.devCost.prop('readonly', parseInt(this.dom.deposit.val()) + parseInt(this.dom.rentCost.val()) > 0);
            this.dom.deposit.add(this.dom.rentCost).prop('readonly', parseInt(this.dom.devCost.val()) > 0);
        },
        initSplitAll: function () {
            this.dom.chargeTime.add(this.dom.remark).parents('tr').hide();
            this.dom.sumCost.add(this.dom.sumDues).prop('readonly', true);
        },
        listener: function () {
            var $this = this;
            //保存，合计金额是否正确
            $this.dom.save.click(function () {
                $this.funObj().submitForm();
            });
            //计算年限
            $this.dom.serCost.on('change keyup', function () {
                $this.funObj().countSerYear();
            });
            //设备费/押金只能输入其一
            $this.dom.devCost.on('change keyup', function () {
                $this.dom.deposit.add($this.dom.rentCost).prop('readonly', !!$(this).val());
            });
            $this.dom.deposit.add($this.dom.rentCost).on('change keyup', function () {
                $this.dom.devCost.prop('readonly', $this.funObj().countRentDeposit());
            });
            //计算结束服务时间
            $this.dom.startTime.on('change', function () {
                $this.funObj().dealExpireTime($(this).val(), $this.dom.serYear.val());
            });
        },
        listenerCountTotal: function () {
            var $this = this;
            //计算总费用及年限
            $this.dom.devCost.add($this.dom.serCost).add($this.dom.deposit).add($this.dom.rentCost).on('change keyup', function () {
                $this.funObj().countTotalCost();
            });
        },
        funObj: function(){
            var $this = this;
            return {
                submitForm: function () {
                    var sumCost   = parseInt($this.dom.sumCost.val()),
                        totalCost = $this.funObj().getTotalCost();
                    if(sumCost <= 0){
                        xhExtra.setBtnNormal($this.dom.save);
                        window.top.xhTop.showMessage({type: 'error', title: '金额不正确！'});
                        return false;
                    }
                    if(sumCost !== totalCost){
                        xhExtra.setBtnNormal($this.dom.save);
                        window.top.xhTop.showMessage({type: 'error', title: '金额合计不正确！'});
                        return false;
                    }
                    xhExtra.setBtnLoading($this.dom.save);
                    $this.form.submit(baseUrl + 'Cost/modifyStudCharge', {type: $this.params.type, schoolId: $this.params.schoolId, ids: $this.params.ids} , function(result){
                        xhExtra.setBtnNormal($this.dom.save);
                        if(xhExtra.checkError(result)) return false;
                        window.top.xhTop.showMessage({type: 'success', title: result.msg});
                        window.top.xhTop.closeModal();
                        window.top.xhTop.getActiveFrame().obj.studDataGrid.reload();
                    });
                },
                countTotalCost: function () {
                    $this.dom.sumDues.add($this.dom.sumCost).val($this.funObj().getTotalCost());
                },
                countSerYear: function () {
                    $this.dom.serYear.val(Math.ceil(parseInt($this.dom.serCost.val()) / $this.params.annualCost))
                },
                getTotalCost: function () {
                    return (parseInt($this.dom.serCost.val()) || 0)
                        + (parseInt($this.dom.devCost.val()) || 0)
                        + (parseInt($this.dom.deposit.val()) || 0)
                        + (parseInt($this.dom.rentCost.val()) || 0);
                },
                countRentDeposit: function () {
                    return  (parseInt($this.dom.deposit.val()) || 0) + (parseInt($this.dom.rentCost.val()) || 0);
                },
                dealExpireTime: function (startTime, year) {
                    if (!year) {
                        return false;
                    }
                    startTime = new Date(startTime);
                    startTime.setFullYear(startTime.getFullYear() + parseInt(year));
                    $this.dom.expireTime.val(xhTool.getNowDate(null, startTime));
                }
            }
        }
    }
}